package com.ciwong.epaper.modules.me.ui.mall;

import android.content.Context;
import android.os.Handler;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.bean.Service;
import com.ciwong.epaper.util.DateUtils;
import com.ciwong.libs.utils.CWLog;

import java.util.Date;

/**
 * Created by neo on 2016/7/13.
 */
public class ItemRemainTime extends LinearLayout {

    private TextView tv_remain_hour1;
    private TextView tv_remain_hour2;

    private TextView tv_remain_min1;

    private TextView tv_remain_min2;

    private TextView tv_remain_second1;

    private TextView tv_remain_second2;

    private View handle_count_down;

    private Handler handler = new Handler();

    private int secondsReduce = 0;

    private long mRemainTime;


    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            secondsReduce++;
            Log.d("ciwong", "#########secondsReduce##########" + secondsReduce);
            mRemainTime = mRemainTime - 1000;
            refreshTimeDis(mRemainTime);
            handler.postDelayed(this, 1000);
        }
    };

    public ItemRemainTime(Context context) {
        super(context);
        LayoutInflater.from(this.getContext()).inflate(R.layout.item_remain_time, this);
        initView();
    }

    public ItemRemainTime(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(this.getContext()).inflate(R.layout.item_remain_time, this);
        initView();
    }


    public ItemRemainTime(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        LayoutInflater.from(this.getContext()).inflate(R.layout.item_remain_time, this);
        initView();
    }

    private void initView() {

        tv_remain_hour1 = (TextView) findViewById(R.id.tv_remain_hour1);
        tv_remain_hour2 = (TextView) findViewById(R.id.tv_remain_hour2);
        tv_remain_min1 = (TextView) findViewById(R.id.tv_remain_min1);
        tv_remain_min2 = (TextView) findViewById(R.id.tv_remain_min2);
        tv_remain_second1 = (TextView) findViewById(R.id.tv_remain_second1);
        tv_remain_second2 = (TextView) findViewById(R.id.tv_remain_second2);
        handle_count_down = findViewById(R.id.handle_count_down);

    }


    public void update(Service service) {

        if (service != null && service.ispromo()) {
            setVisibility(VISIBLE);
            Date endTime = DateUtils.String2Date(DateUtils.DATE_LONG, service.getPromotion().getEndTime());
            Date nowTime = new Date();
            CWLog.d("ciwong", "#########service.getPromotion().getEndTime()########" + service.getPromotion().getEndTime());
            mRemainTime = endTime.getTime() - nowTime.getTime();

            refreshTimeDis(mRemainTime);
            handler.postDelayed(runnable, 1000);
        } else {
            setVisibility(GONE);
        }

    }


    private void refreshTimeDis(long remainTime) {
        String remainDay = String.format("%02d", remainTime / (24 * 60 * 60 * 1000));

        long dayToTime = (remainTime % (24 * 60 * 60 * 1000) / (60 * 60 * 1000)) + remainTime / (24 * 60 * 60 * 1000) * 24;
        String remainHours = String.format("%02d", dayToTime);
        String remainMin = String.format("%02d", (remainTime % (60 * 60 * 1000)) / (60 * 1000));
        String remainSecond = String.format("%02d", (remainTime % (60 * 1000)) / 1000);
        handle_count_down.setVisibility(VISIBLE);
        tv_remain_hour1.setText("");
        tv_remain_hour2.setText(dayToTime + "");
        tv_remain_min1.setText(remainMin.charAt(0) + "");
        tv_remain_min2.setText(remainMin.charAt(1) + "");
        tv_remain_second1.setText(remainSecond.charAt(0) + "");
        tv_remain_second2.setText(remainSecond.charAt(1) + "");
//        }


    }


    public void onDestroy() {
        //不销毁掉会一直在后台运行
        handler.removeCallbacks(runnable);
    }
}
